package com.jeesite.modules.rt.web;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.mo.service.MoUserDeviceService;
import com.jeesite.modules.rt.dao.RtDailiDeviceDao;
import com.jeesite.modules.rt.entity.RtDevice;
import com.jeesite.modules.rt.service.RtDeviceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.rt.entity.RtDailiDevice;
import com.jeesite.modules.rt.service.RtDailiDeviceService;

/**
 * rt_daili_deviceController
 * @author 李寒冰
 * @version 2024-12-17
 */
@Controller
@RequestMapping(value = "${adminPath}/rt/rtDailiDevice")
public class RtDailiDeviceController extends BaseController {

	@Autowired
	private RtDailiDeviceService rtDailiDeviceService;

	@Autowired
	private MoUserDeviceService moUserDeviceService;
    @Autowired
    private RtDeviceService rtDeviceService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public RtDailiDevice get(String id, boolean isNewRecord) {
		return rtDailiDeviceService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("rt:rtDevice:view")
	@RequestMapping(value = {"list", ""})
	public String list(RtDailiDevice rtDailiDevice, Model model) {
		model.addAttribute("rtDailiDevice", rtDailiDevice);
		return "modules/rt/rtDailiDeviceList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("rt:rtDevice:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<RtDailiDevice> listData(RtDailiDevice rtDailiDevice, HttpServletRequest request, HttpServletResponse response) {
		rtDailiDevice.setPage(new Page<>(request, response));
		Page<RtDailiDevice> page = rtDailiDeviceService.findPage(rtDailiDevice);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("rt:rtDevice:view")
	@RequestMapping(value = "form")
	public String form(RtDailiDevice rtDailiDevice, Model model) {
		model.addAttribute("rtDailiDevice", rtDailiDevice);
		return "modules/rt/rtDailiDeviceForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("rt:rtDevice:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(RtDailiDevice rtDailiDevice) {
		String sn = rtDailiDevice.getSn();
		if(sn!=null && sn.trim().length()<=0){
			return renderResult(Global.FALSE, text("请填写设备SN号！"));
		}
		if(sn.startsWith("http") && sn.indexOf("?sn=")!=-1){
			String[] split = sn.split("sn=");
			sn = split[1];
		}
		sn = sn.trim();


		RtDevice deviceBySn = rtDeviceService.getDeviceBySn(sn);
		if(deviceBySn==null){
			return renderResult(Global.FALSE, text("此设备不存在！"));
		}

		RtDailiDevice oneBySn = rtDailiDeviceService.getOneBySn(sn);
		if (oneBySn != null) {
			return renderResult(Global.FALSE, text("此设备已绑定代理商！"));
		}

		if (moUserDeviceService.findBySn(sn)!=null){
			return renderResult(Global.FALSE, text("此设备已绑定用户，请先解绑！"));
		}
		rtDailiDevice.setSn(sn);
		rtDailiDeviceService.save(rtDailiDevice);
		return renderResult(Global.TRUE, text("保存rt_daili_device成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("rt:rtDevice:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(RtDailiDevice rtDailiDevice) {
		rtDailiDeviceService.delete(rtDailiDevice);
		return renderResult(Global.TRUE, text("删除rt_daili_device成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("rt:rtDevice:edit")
	@RequestMapping(value = "deleteSn")
	@ResponseBody
	public String deleteSn(RtDailiDevice rtDailiDevice) {
		String sn = rtDailiDevice.getSn();
		RtDailiDevice oneBySn = rtDailiDeviceService.getOneBySn(sn);
		if (oneBySn != null) {
			rtDailiDeviceService.delete(oneBySn);
		}
		return renderResult(Global.TRUE, text("解绑成功！"));
	}

}